package com.woniuxy.handler;

/**
 * @author Administrator
 * @title: OrderHandler
 * @projectName mashangpai
 * @description: TODO
 * @date 2019/9/1217:20
 */

import com.github.pagehelper.PageInfo;
import com.woniuxy.Uitl.OrderUtil;
import com.woniuxy.dto.OrderDTO;
import com.woniuxy.service.OrderService;
import com.work.framework.pojo.Order;
import com.work.framework.pojo.ResultDTO;
import com.work.framework.pojo.Site;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.ietf.jgss.Oid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.websocket.Session;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单类
 */
@RestController
@ResponseBody
@RequestMapping("/order")
@Api(value = "订单类", tags = "订单")
public class OrderHandler {

    @Autowired
    private OrderService orderService;

    private static OrderUtil orderUtil = new OrderUtil();

    @ApiOperation(value = "订单模块", notes = "添加订单（bid：买家ID，gid：商品ID，TransactionPerice：成交价）")
    @PostMapping("/addOrder/{bid}/{gid}/{transacPrice}")
    public Integer addOrder(@PathVariable("bid") Integer bid, @PathVariable("gid") Integer gid, @PathVariable("transacPrice") BigDecimal TransactionPerice) {
        Order order = new Order();
        order.setBid(bid);
        order.setGid(gid);
        order.setTransactionPerice(TransactionPerice);
        Integer code = orderService.addOrder(order);
        return code;
    }

    @ApiOperation(value = "订单模块", notes = "订单支付界面信息")
    @PostMapping("/findOrder")
    public ResultDTO findOrder(Integer oid) {
        OrderDTO orderDTO = orderService.findOrderDTOByOId(oid);
        return new ResultDTO(200, "订单支付信息", orderDTO);
    }

    @ApiOperation(value = "订单模块", notes = "订单详细信息")
    @PostMapping("/orderMassage")
    public ResultDTO orderMassage(Integer oid) {
        Map<String, Object> map = orderService.findMassage(oid);
        return new ResultDTO(200, "订单详细信息", map);
    }

    @ApiOperation(value = "订单模块", notes = "买家订单信息(page:当前页数，limit:当前页容量)")
    @GetMapping("/buyerOrder")
    public ResultDTO buyerOrder(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                @RequestParam(value = "limit", defaultValue = "5") Integer limit,HttpServletRequest request) {
        HttpSession session = request.getSession();
        List<OrderDTO> orderDTOS = orderService.searchBayerOrderDTO(page, limit,session);
        PageInfo<OrderDTO> pageInfo = new PageInfo<>(orderDTOS);
        return new ResultDTO(200, "买家分页订单信息", orderDTOS, pageInfo.getTotal());
    }

    @ApiOperation(value = "订单模块", notes = "买家订单信息(page:当前页数，limit:当前页容量)")
    @GetMapping("/sellerOrder")
    public ResultDTO sellerOrder(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "limit", defaultValue = "5") Integer limit, HttpServletRequest request) {
        HttpSession session = request.getSession();
        List<OrderDTO> orderDTOS = orderService.searchSellerOrderDTO(page, limit, session);
        PageInfo<OrderDTO> pageInfo = new PageInfo<>(orderDTOS);
        return new ResultDTO(200, "买家分页订单信息", orderDTOS, pageInfo.getTotal());
    }

    @ApiOperation(value = "订单模块", notes = "后台订单信息(page:当前页数，limit:当前页容量,orderNum:订单编号,cname:商品类型,gname:商品名字)")
    @GetMapping("/showOrder")
    public ResultDTO sellerOrder(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                 @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                 OrderDTO orderDTO) {
        List<OrderDTO> orderDTOS = orderService.searchOrderDTO(page, limit, orderDTO);
        PageInfo<OrderDTO> pageInfo = new PageInfo<>(orderDTOS);
        return new ResultDTO(0, "后台分页订单信息", orderDTOS, pageInfo.getTotal());
    }


    @ApiOperation(value = "订单模块", notes = "添加快递单号")
    @PostMapping("/addExpressNum")
    public ResultDTO addExpressNum(String expressNum, Integer oid) {
        Integer code = orderService.addExpressNum(expressNum, oid);
        String massage = orderUtil.state(code);
        return new ResultDTO(200, massage);
    }

    @ApiOperation(value = "订单模块", notes = "完成订单")
    @PostMapping("/overOrder")
    public ResultDTO overOrder(Integer oid) {
        Integer code = orderService.overOrder(oid);
        String massage = orderUtil.state(code);
        return new ResultDTO(code, massage);
    }

    @ApiOperation(value = "订单模块", notes = "订单状态修改")
    @PostMapping("/deleteOrder")
    public ResultDTO deleteOrder(Integer flage, Integer oid) {
        Integer code = orderService.updateOrder(oid, flage);
        String massage = orderUtil.state(code);
        return new ResultDTO(code, massage);
    }

}
